<?php
///////////////////////////////////////////////////////////////////////////////
/*
company.php

Class Company - class containing methods for retrieving and saving company data.


*/

class Company {

    private $id;
    private $name;
    private $address1;
    private $address2;
    private $city;
    private $postalcode;
    private $localemployeecount;
    private $yearfounded;
    private $webaddress;
    private $description;

    public static $TABLE_COMPANY = TABLE_OBJECT;

    /////////////////////////////////////////////////////////////////////////////
    // contructor - instantiates company object with supplied attributes
    public function __construct($id = null, $name = null, $address1 = null, $address2 = null,
                                $city = null, $postalcode = null, $localemployeecount = null,
                                $yearfounded = null, $webaddress = null, $description = null) {
        $this->init($id, $name, $address1, $address2, $city, $postalcode, $localemployeecount,
                    $yearfounded, $webaddress, $description);
    }

    /////////////////////////////////////////////////////////////////////////////
    // init - convenience function to set all company attribute values at once
    public function init($id = null, $name = null, $address1 = null, $address2 = null,
                         $city = null, $postalcode = null, $localemployeecount = null,
                         $yearfounded = null, $webaddress = null, $description = null) {
        $this->setID($id);
        $this->setName($name);
        $this->setAddress1($address1);
        $this->setAddress2($address2);
        $this->setCity($city);
        $this->setPostalcode($postalcode);
        $this->setLocalemployeecount($localemployeecount);
        $this->setYearfounded($yearfounded);
        $this->setWebaddress($webaddress);
        $this->setDescription($description);
    }

    /////////////////////////////////////////////////////////////////////////////
    // load - initialize the company by loading it from the database based on the
    // specificed id
    public function load($id) {
        $sql = "SELECT id, name, address1, address2, city, postalcode, localemployeecount, " .
               "yearfounded, webaddress, description " .
             "FROM " . Company::$TABLE_COMPANY . " where id = " . $id;
        $result = DatabaseManager::getInstance()->query($sql);

        if ($row = $result->fetch_assoc()) {
            $this->init($row['id'], $row['name'], $row['address1'], $row['address2'],
                        $row['city'], $row['postalcode'], $row['localemployeecount'],
                        $row['yearfounded'], $row['webaddress'], $row['description']);
        } else {
            return false;
        }
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // insert - insert a new company into the database and return the new Company
    // Don't insert if it already exists.  That is, name already exists.
    public function insert() {

        if ($this->existsInDatabase()) {
            // print("company already exists in database\n");
            return false;
        }

        $sql = "CALL insertCompany('{$this->getName()}', " .
                                   "'{$this->getAddress1()}', " .
                                   "'{$this->getAddress2()}', " .
                                   "'{$this->getCity()}', " .
                                   "'{$this->getPostalcode()}', " .
                                   "{$this->getLocalemployeecount()}, " .
                                   "{$this->getYearfounded()}, " .
                                   "'{$this->getWebaddress()}', " .
                                   "'{$this->getDescription()}', " .
                               "@newCompanyID)";
        $result = DatabaseManager::getInstance()->query($sql);
        $sql = "select @newCompanyID";
        $result = DatabaseManager::getInstance()->query($sql);
        if ($row = $result->fetch_assoc()) {
            $this->setID($row["@newCompanyID"]);
        } else {
            return false;
        }
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // update - update the company to the database (company already exists in database)
    public function update() {

        $sql = "UPDATE " . Company::$TABLE_COMPANY .
                              " set name = '{$this->getName()}', " .
                                   "address1 = '{$this->getAddress1()}', " .
                                   "address2 = '{$this->getAddress2()}', " .
                                   "city = '{$this->getCity()}', " .
                                   "postalcode = '{$this->getPostalcode()}', " .
                                   "localemployeecount = {$this->getLocalemployeecount()}, " .
                                   "yearfounded = {$this->getYearfounded()}, " .
                                   "webaddress = '{$this->getWebaddress()}', " .
                                   "description = '{$this->getDescription()}' " .
                                   "WHERE id = {$this->getID()}";
        $result = DatabaseManager::getInstance()->query($sql);
    }

    /////////////////////////////////////////////////////////////////////////////
    // delete - delete the company from the database
    public function delete() {
        $sql = "DELETE FROM " . Company::$TABLE_COMPANY . " WHERE id = {$this->getID()}";
        $result = DatabaseManager::getInstance()->query($sql);
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // existsInDatabase - checks to see if company already exists in database -
    // meaning a company record with name equal to the name in this Company object.
    private function existsInDatabase() {
        $companyCount = 0;
        $sql = "SELECT count(*) as company_count FROM " . Company::$TABLE_COMPANY .
               " where name = '{$this->getName()}' ";
        $result = DatabaseManager::getInstance()->query($sql);
        if ($row = $result->fetch_assoc()) {
            $companyCount = $row['company_count'];
        } else {
            // error handling?
        }
        if ($companyCount < 1) {
            return false;
        }
        return true;
    }


    /////////////////////////////////////////////////////////////////////////////
    // getID - get the node id
    public function getID() {
        return $this->id;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setID - set the node id
    public function setID($id) {
        $this->id = $id;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getName - get the name
    public function getName() {
        return $this->name;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setName - set the name
    public function setName($name) {
        $this->name = $name;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getAddress1 - get address1
    public function getAddress1() {
        return $this->address1;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setAddress1 - set address1
    public function setAddress1($address1) {
        $this->address1 = $address1;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getAddress2 - get address2
    public function getAddress2() {
        return $this->address2;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setAddress2 - set address2
    public function setAddress2($address2) {
        $this->address2 = $address2;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getCity - get the city
    public function getCity() {
        return $this->city;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setCity - set the city
    public function setCity($city) {
        $this->city = $city;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getPostalcode - get the postal code
    public function getPostalcode() {
        return $this->postalcode;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setPostalcode - set the postal code
    public function setPostalcode($postalcode) {
        $this->postalcode = $postalcode;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getLocalemployeecount - get the local employee count
    public function getLocalemployeecount() {
        return $this->localemployeecount;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setLocalemployeecount - set the local employee count
    public function setLocalemployeecount($localemployeecount) {
        $this->localemployeecount = $localemployeecount;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getYearfounded - get the year founded
    public function getYearfounded() {
        return $this->yearfounded;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setYearfounded - set the year founded
    public function setYearfounded($yearfounded) {
        $this->yearfounded = $yearfounded;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getWebaddress - get the web address
    public function getWebaddress() {
        return $this->webaddress;
    }
    /////////////////////////////////////////////////////////////////////////////
    // setWebaddress - set the web address
    public function setWebaddress($webaddress) {
        $this->webaddress = $webaddress;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getDescription - get the description
    public function getDescription() {
        return $this->description;
    }
    /////////////////////////////////////////////////////////////////////////////
    // setDescription - set the description
    public function setDescription($description) {
        $this->description = $description;
    }



    /////////////////////////////////////////////////////////////////////////////
    // printCompanyData - debug function that dumps company attributes
    public function printCompanyData($desc = "") {
        //print("--------------------------------------------\n");
        print("{$desc}:\n");
        print("\tID: {$this->getID()}\n");
        print("\tNAME: {$this->getName()}\n");
        print("\tADDRESS 1: {$this->getAddress1()}\n");
        print("\tADDRESS 2: {$this->getAddress2()}\n");
        print("\tCITY: {$this->getCity()}\n");
        print("\tPOSTAL CODE: {$this->getPostalcode()}\n");
        print("\tLOCAL EMPLOYEE COUNT: {$this->getLocalemployeecount()}\n");
        print("\tYEAR FOUNDED: {$this->getYearfounded()}\n");
        print("\tWEB ADDRESS: {$this->getWebaddress()}\n");
        print("\tDESCRIPTION: {$this->getDescription()}\n");
    }





    // Static functions - mainly for retrieving groups of companies

    /////////////////////////////////////////////////////////////////////////////
    public static function loadCompanies($condition) {
        $companies = array();

        $sql = "SELECT id, name, address1, address2, city, postalcode, localemployeecount, " .
               "yearfounded, webaddress, description FROM " .
               Company::$TABLE_COMPANY . " where " .
               $condition;               


        $result = DatabaseManager::getInstance()->query($sql);
        while ($row = $result->fetch_assoc()) {
            $company = new Company($row['id'],
                             $row['name'],
                             $row['address1'],
                             $row['address2'],
                             $row['city'],
                             $row['postalcode'],
                             $row['localemployeecount'],
                             $row['yearfounded'],
                             $row['webaddress'],
                             $row['description']);
            $companies[] = $company;
        }
        return $companies;
    }


    /////////////////////////////////////////////////////////////////////////////
    public static function loadAllCompanies() {
        return Company::loadCompanies("1");
    }






    /////////////////////////////////////////////////////////////////////////////
    public static function printCompaniesData($companies, $desc = "") {
        print($desc . "\n");
        $i = 0;
        foreach ($companies as $company) {
            $i++;
            $company->printCompanyData("Company " . $i);
        }
        print(count($companies) . " companies\n");
    }




}

